package audit

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/rpc-client/agentClient/rpc"
	"github.com/spf13/cast"
	"github.com/zeromicro/go-zero/core/logc"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type AuditListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 代理审核列表
func NewAuditListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AuditListLogic {
	return &AuditListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AuditListLogic) AuditList(req *types.AuditListRequest) (resp []*types.AuditListResponse, err error) {
	list, err := l.svcCtx.AgentRPC.AuditList(l.ctx, &rpc.AgentAuditListRequest{
		AgentId:                    req.AgentId,
		SubAgentFirstChargeDaysMin: req.SubAgentFirstChargeDaysMin,
		SubAgentFirstChargeDaysMax: req.SubAgentFirstChargeDaysMax,
		AgentType:                  cast.ToInt64(req.AgentType),
		RebatesStatus:              cast.ToInt64(req.RebatesStatus),
	})
	if err != nil {
		logc.Error(l.ctx, err)
		return nil, err
	}
	_data := make([]*types.AuditListResponse, len(list.Data))
	for _i, _row := range list.Data {
		_data[_i] = &types.AuditListResponse{
			AgentId:                 _row.AgentId,
			ParentId:                _row.ParentId,
			ImmediateSubordinate:    _row.ImmediateSubordinate,
			AgentType:               _row.AgentType,
			SubAgentFirstChargeDays: _row.SubAgentFirstChargeDays,
			RebatesStatus:           _row.RebatesStatus,
			YesterdayRebates:        _row.YesterdayRebates,
			SubOrderTotal:           _row.SubOrderTotal,
			LatestHandleUser:        _row.LatestHandleUser,
			LatestHandleTime:        _row.LatestHandleTime,
			LatestStatusHandleUser:  _row.LatestStatusHandleUser,
			RebatesSettingStatus:    _row.RebatesSettingStatus,
		}
	}
	return _data, nil
}
